package com.zjc.yohoshop.mvc.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.zjc.yohoshop.R;
import com.zjc.yohoshop.mvc.view.adapter.ImageAdapter;

import java.util.ArrayList;
import java.util.List;

public class ImageActivity extends AppCompatActivity {

    private ViewPager vp_image;
    private ImageAdapter adapter;
    private boolean touch = false;
    private LinearLayout ll_image_dian;
    private List<ImageView> imageViews = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_image);
        initView();
        initListener();
    }

    //添加点击事件
    private void initListener() {
//        vp_image.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                switch (event.getAction()) {
//                    case MotionEvent.ACTION_DOWN:
//                        touch = true;
//                        break;
//                    case MotionEvent.ACTION_MOVE:
//                        touch = false;
//                        break;
//                    case MotionEvent.ACTION_UP:
//                        if (touch) {
//                            finish();
//                        }
//                        break;
//                }
//                return false;
//            }
//        });


        vp_image.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
                for (int j = 0; j < imageViews.size(); j++){
                    imageViews.get(j).setImageResource(R.drawable.tt_preview_dot_down);
                }
                imageViews.get(i).setImageResource(R.drawable.icon_dot);
            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });

    }

    //初始化控件
    private void initView() {
        vp_image = (ViewPager) findViewById(R.id.vp_image);
        ll_image_dian = (LinearLayout) findViewById(R.id.ll_image_dian);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        ArrayList<String> images = bundle.getStringArrayList("images");
        adapter = new ImageAdapter(images);
        vp_image.setAdapter(adapter);

        for (int i = 0; i < images.size(); i++){
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.drawable.tt_preview_dot_down);
            imageView.setPadding(10, 10, 10, 10);
            imageViews.add(imageView);
            ll_image_dian.addView(imageView);
        }

    }
}
